/**
 * 
 */
package com.iuie.ns.system.controller.user;

import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.user.ISysUserService;
import com.iuie.ns.system.vo.user.SysUserVO;

/**
 * @description 系统用户管理
 * @author lj
 * @createTime 2016年9月7日 上午10:04:20
 */
@Controller
@RequestMapping("sys/user")
public final class SysUserController {
	
	@Resource
	private ISysUserService sysUserService;
	
	@RequestMapping
	public String index(){
		return "/view-system/user/user";
	}
	@RequestMapping("add/index")
	public String addIndex(){
		return "/view-system/user/userAdd";
	}
	
	@RequestMapping("update/index/{userId}")
	public String updateIndex(HttpServletRequest request, @PathVariable("userId") String userId) {
		request.setAttribute("userId", userId);
		return "/view-system/user/userUpdate";
	}
	
	@RequestMapping(value = "/addOne", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
	@ResponseBody
	public String addOneUser(SysUserVO user, @RequestParam(value="roleIds[]", required= false) String[] roleIds, 
			HttpServletRequest req) {
		ResponseInfo ri = new ResponseInfo();
		SysUserVO newUser = sysUserService.addOneUser(user);
		sysUserService.saveUserRole(newUser.getUserId(), roleIds);
		ri.setMessage("用户：" + newUser.getUserId() + " 新增成功！");
		String json = ri.toJson();
		return json;
	}

	@RequestMapping(value = "/updateOne/{userId}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseInfo updateOneUser(@PathVariable("userId") String userId, SysUserVO user, @RequestParam(value="roleIds[]", required= false) String[] roleIds, 
			HttpServletRequest req) {
		ResponseInfo ri = new ResponseInfo();
		sysUserService.updateOneUser(user);
		sysUserService.saveUserRole(user.getUserId(), roleIds);
		ri.setMessage("用户：" + user.getUserId() + " 修改成功！");
		return ri;
	}
	
	@RequestMapping(value = "/deleteOne", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
	@ResponseBody
	public String deleteOneUser(String username, HttpServletRequest req) {
		sysUserService.deleteById(username);
		return "true";
	}
	
	@RequestMapping(value="/find", method=RequestMethod.GET)
	@ResponseBody
	public ResponseInfo findByCondition(SysUserVO user,Page page) throws JsonProcessingException {
		List<SysUserVO> list = sysUserService.findByPage(user,page);
		ResponseInfo ri = new ResponseInfo(page, list);
		return ri;
	}

	@RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
	public void exportExcel(HttpServletResponse resp) {
		resp.setContentType("application/vnd.ms-excel");
		// 进行转码，使其支持中文文件名
		String codedFileName;
		try {
			codedFileName = java.net.URLEncoder.encode(ISysUserService.SYS_USER_BOOK_NAME, "UTF-8");
			resp.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
			OutputStream out = resp.getOutputStream();
			sysUserService.exportExcel(out,null);
		} catch (Exception e) {
			System.out.println("导出Excel失败");
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/findOne/{userId}", method=RequestMethod.GET)
	@ResponseBody
	public ResponseInfo findById(@PathVariable("userId") String userId) {
		SysUserVO user = sysUserService.findByIdWithRoles(userId);
		ResponseInfo ri = new ResponseInfo();
		ri.setInfo(user);
		ri.setMessage("查询成功");
		return ri;
	}

	@RequestMapping(value="/resetPwd/{userId}", method=RequestMethod.POST)
	@ResponseBody
	public ResponseInfo resetUserPwd(@PathVariable("userId") String userId) {
		sysUserService.resetUserPwd(userId);
		ResponseInfo ri = new ResponseInfo();
		ri.setMessage("用户【" + userId + "】的密码重置成功！");
		return ri;
	}
}
